﻿using Hotel.DAL;
using Hotel.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.BLL
{
    public class MemberBLL
    {
        public MemberBLL(MemberDAL memberDAL)
        {
            MemberDAL = memberDAL;
        }

        public MemberDAL MemberDAL { get; }




        /// <summary>
        /// 会员增加
        /// </summary>
        /// <param name="info">会员实体</param>
        /// <returns>返回影响的行数</returns>
        public int MemberAdd(MemberModel info)
        {
            try
            {
                //1.判断手机和身份证号码是否重复
                int res = MemberDAL.SearchPhoneOrCard(info.Phone, info.Card);
                if (res == -1)
                {
                    return -1; //表示手机号重复

                }
                else if (res == -2)
                {
                    return -2;  //表示身份证号码重复
                }
                else
                {
                    //2.增加成功
                    return MemberDAL.MemberAdd(info);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 会员等级的增加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int AddMemberLevel(MemberLevelModel info)
        {
            try
            {
                //1.等级名称是否重复
                int res = MemberDAL.SearchLevelName(info.LevelName);
                if (res > 0)
                {
                    return -1;//表示重复
                }
                else
                {
                    int r = MemberDAL.AddMemberLevel(info);
                    if (r > 0)
                    {
                        return 1;//表示增加成功
                    }
                    else
                    {
                        return -2;//增加失败
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取会员等级列表
        /// </summary>
        /// <returns></returns>
        public List<MemberLevelModel> MemberLevelsList()
        {
            try
            {
                return MemberDAL.MemberLevelsList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取会员列表
        /// </summary>
        /// <param name="id">会员编号</param>
        /// <param name="name">会员名称</param>
        /// <param name="levelId">等级名称</param>
        /// <returns></returns>

        public List<MemberModel> MemberList(int? id = null, string? name = "", int? levelId = null)
        {
            try
            {
                //查询所有显示的数据
                var list = MemberDAL.MemberList(id, name, levelId);
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据id删除会员信息
        /// </summary>
        /// <param name="id">会员id</param>
        /// <returns>返回影响的行数</returns>
        public int DelMemeberItem(int id)
        {
            try
            {
                int res = MemberDAL.DelMemeberItem(id);
                if (res > 0)
                {
                    return 1;//表示删除成功
                }
                else
                {
                    return -2;//表示删除失败
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据会员id获取会员实体 返填+详情展示
        /// </summary>
        /// <param name="id">会员id</param>
        /// <returns></returns>
        public MemberModel GetMemberInfo(int id)
        {
            try
            {
                //根据id获取会员实体
                var info = MemberDAL.GetMemberInfo(id);
                return info;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改会员
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int UpdatMemberInfo(MemberModel info)
        {
            try
            {
                int res = MemberDAL.UpdatMemberInfo(info);
                if (res > 0)
                {
                    return 1;//修改成功
                }
                else
                {
                    return -2;//修改失败
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }









        /// <summary>
        /// 会员权益添加
        /// </summary>
        /// <param name="memberRight"></param>
        /// <returns></returns>
        public int MemberRightAdd(MemberRight memberRight)
        {
            int res = MemberDAL.MemberRightName(memberRight.RightName);
            if (res>0)
            {
                return -1;//重复了
            }
            else
            {
                int r = MemberDAL.MemberRightAdd(memberRight);
                if (r>0)
                {
                    return 1;//添加成功
                }
                else 
                {
                    return -2;//添加失败
                }
            }
        }












        /// <summary>
        /// 会员权益显示
        /// </summary>
        /// <param name="count">总条数</param>
        /// <param name="page">总页码</param>
        /// <param name="rightName">名称</param>
        /// <param name="createtiem">时间</param>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <returns></returns>
        public List<MemberRight> MemberRightLiist(out int count, out int page, string? rightName = "", DateTime? date1 = null, DateTime? date2 = null, int index = 1, int size = 2)
        {


            List<MemberRight> list = MemberDAL.MemberRightLiist(out count, out page, rightName, date1, date2, index, size);
            return list;

        }





        /// <summary>
        /// 修改会员权益的状态
        /// </summary>
        /// <param name="id">权益id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public int UpdateMemberRightState(int id, int state)
        {
            int res = MemberDAL.UpdateMemberRightState(id, state);
            if (res>0) 
            {
                return 1;//成功
            }

            else
            {
                 return -2;//失败
            }
        }



















    }
}
